;*****************************************************
; 	Copyright (C) SEIKO EPSON CORP. 1997-2001
;	All rights Reserved
;
;	File name : strstr.s
;	Function  :
;	       This is strstr function
;	Revision  :
;		1997/03/25		V.Chan start
;		1997/03/26		optimized
;		2001/12/06		T.Tazaki for GNU33(Linux)
;		2002/06/06		T.Tazaki bug fixed
;*****************************************************

;*****************************************************
; strstr
;	return first occurrence in src1 (0 terminated)
;	of src2 (0 terminated)
;
; arguments : %r6:src1 addr, %r7:src2 addr
; return    : %r4:addr of 1st occurrence
; comment   : destroy %r8,%r9
;*****************************************************

	.section .text
	.align 1
	.global strstr
	.type	strstr,@function

strstr:
	ld.ub	%r10, [%r7]+	; load first char of src2
	ld.w	%r9, %r7		; save addr

	cmp     %r10,0			;add T.Tazaki '02.06.05 
	jreq.d  strstr_ret		;add T.Tazaki '02.06.05 
	ld.w    %r4,%r6			;add T.Tazaki '02.06.05 

strstr_loop:
	ld.ub	%r11, [%r6]+	; load byte from src1
strstr_loop_2:				;add T.Tazaki '02.06.05 

	cmp		%r10, %r11		; compare first byte of src2
	jreq.d	equal
	ld.w	%r4, %r6		; set result

;	; case: bytes are not equal	
;	cmp		%r10, 0			; src2 is empty	; del T.Tazaki 2002.06.06
;	jreq	equal			; del T.Tazaki 2002.06.06
	cmp		%r11, 0			; exit when src1 is done

;	jreq.d	strstr_ret		; del T.Tazaki 2002.06.06
;	ld.w	%r4, 0			; return NULL ; del T.Tazaki 2002.06.06
	jreq	strstr_ret_2	; add T.Tazaki '02.06.06
	jp		strstr_loop

equal:
	sub		%r4, 1			; get starting addr

;strcmp_loop:				; del T.Tazaki 2002.06.06
	cmp		%r11, 0			; exit if both bytes = 0
	jreq	strstr_ret

str_cmp_loop2:				; add T.Tazaki '02.06.05
	ld.ub	%r8, [%r9]+		; load byte from src2
	ld.ub	%r11, [%r6]+	; load byte from src1
	cmp		%r8, 0			; add T.Tazaki '02.06.06
	jreq	strstr_ret		; add T.Tazaki '02.06.06
	cmp		%r11, 0			; add T.Tazaki '02.06.06
	jreq	strstr_ret_2	; add T.Tazaki '02.06.06
	cmp		%r8, %r11
;	jreq	strcmp_loop		; del T.Tazaki '02.06.06
	jreq	str_cmp_loop2	; add T.Tazaki '02.06.06

	ld.w	%r9, %r7		; reset temp

;	jp.d	strstr_loop		; del T.Tazaki '02.06.06
	jp.d	strstr_loop_2	; add T.Tazaki '02.06.06

strstr_ret_2:				; add T.Tazaki '02.06.06
	ld.w	%r4, 0			; clear result
strstr_ret:
	ret

